﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using AGE.Rendering;
using Microsoft.Xna.Framework.Content;

namespace AGE.Scene
{
    public abstract class Scene
    {
        
        private Camera currentCamera;
   
        private Dictionary<string, Entity> entities = new Dictionary<string, Entity>();

        public int EntityCount
        {
            get { return entities.Count; }
        }

        public List<Entity> Entities()
        {
            return entities.Values.ToList();
        }

        public void AddEntity(params string[] entityNames)
        {
            for (int i = 0; i < entityNames.Length; i++)
            {
                AddEntity(entityNames[i]);
            }
        }

        public void AddEntity(Entity[] entities)
        {
            for (int i = 0; i < entities.Length; i++)
            {
                AddEntity(entities[i]);
            }
        }
        
        public void AddEntity(Entity entity)
        {
            entity.ParentScene = this;
            entities.Add(entity.Name, entity);
        }

        public void AddEntity(string name)
        {
            AddEntity(new Entity(name));
        }

        

        public Camera CurrentCamera
        {
            get { return currentCamera; }
            set { currentCamera = value; }
        }

        public void Clear()
        {
            entities.Clear();
        }

        public Entity Find(string entityName)
        {
            if (entities.ContainsKey(entityName))
            {
                return entities[entityName];
            }
            else
            {
                return null;
            }
        }

        public abstract void LoadContent(ContentManager contentManager);
        
        public virtual void Update(float deltaTime)
        {
            for (int i = 0; i < entities.Count; i++)
            {
                entities.ElementAt(i).Value.Update(deltaTime);
            }
        }

        public virtual void Initialize(GraphicsDevice graphicsDeviceIn)
        {
            for (int i = 0; i < entities.Count; i++)
            {
                entities.ElementAt(i).Value.Initialize(graphicsDeviceIn);
            }
        }

        public virtual void Draw(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < entities.Count; i++)
            {
                
                entities.ElementAt(i).Value.Draw(graphicsDevice, currentCamera.View, currentCamera.Projection);
            }
        }
    }
}
